package com.demo.service.imp;

import cn.hutool.crypto.SecureUtil;
import com.demo.dao.IUserDao;
import com.demo.dao.impl.UserDaoImpl;
import com.demo.pojo.User;
import com.demo.service.UserService;

import java.util.UUID;

public class UserServiceImpl implements UserService {
    private IUserDao userDao = new UserDaoImpl();
    /**
     * 处理登录的逻辑
     */
    public User login(String name, String password){
        try{
            User user = userDao.login(name,password);
            return user;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 处理注册的逻辑
     */
    public Boolean register(User user){
        try{
            // 注册的时候,需要针对每个账号生成唯一的标识用于加密的盐
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            user.setSalt(uuid);
            // 需要对注册的密码进行摘要加密(md5)
            String pwd = SecureUtil.md5(user.getPassword()+user.getSalt());
            user.setPassword(pwd);
            // 调用dao层方法
            int x = userDao.register(user);
            return x > 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
